1 using System;
2 using
UnityEngine;
3 using
UnityStandardAssets.CrossPlatformInput.PlatformSpecific;
4
5 namespace
UnityStandardAssets.CrossPlatformInput
6 {
7     
public static class CrossPlatformInputManager
8     {
9         
public enum ActiveInputMethod
10         {
11             Hardware,
12             Touch
13         }
14
15
16         
private static VirtualInput activeInput;
17
18         
private static VirtualInput s_TouchInput;
19         
private static VirtualInput s_HardwareInput;
20
21
22         
static CrossPlatformInputManager()
23         {
24             s_TouchInput =
new MobileInput();
25             s_HardwareInput =
new StandaloneInput();
26 #
if MOBILE_INPUT
27             activeInput = s_TouchInput;
28 #
else
29             activeInput = s_HardwareInput;
30 #endif
31         }
32
33         
public static void SwitchActiveInputMethod(ActiveInputMethod activeInputMethod)
34         {
35             
switch (activeInputMethod)
36             {
37                 
case ActiveInputMethod.Hardware:
38                     activeInput = s_HardwareInput;
39                     
break;
40
41                 
case ActiveInputMethod.Touch:
42                     activeInput = s_TouchInput;
43                     
break;
44             }
45         }
46
47         
public static bool AxisExists(string name)
48         {
49             
return activeInput.AxisExists(name);
50         }
51
52         
public static bool ButtonExists(string name)
53         {
54             
return activeInput.ButtonExists(name);
55         }
56
57         
public static void RegisterVirtualAxis(VirtualAxis axis)
58         {
59             activeInput.RegisterVirtualAxis(axis);
60         }
61
62
63         
public static void RegisterVirtualButton(VirtualButton button)
64         {
65             activeInput.RegisterVirtualButton(button);
66         }
67
68
69         
public static void UnRegisterVirtualAxis(string name)
70         {
71             
if (name == null)
72             {
73                 
throw new ArgumentNullException("name");
74             }
75             activeInput.UnRegisterVirtualAxis(name);
76         }
77
78
79         
public static void UnRegisterVirtualButton(string name)
80         {
81             activeInput.UnRegisterVirtualButton(name);
82         }
83
84
85         
// returns a reference to a named virtual axis if it exists otherwise null
86         
public static VirtualAxis VirtualAxisReference(string name)
87         {
88             
return activeInput.VirtualAxisReference(name);
89         }
90
91
92         
// returns the platform appropriate axis for the given name
93         
public static float GetAxis(string name)
94         {
95             
return GetAxis(name, false);
96         }
97
98
99         
public static float GetAxisRaw(string name)
100         {
101             
return GetAxis(name, true);
102         }
103
104
105         
// private function handles both types of axis (raw and not raw)
106         
private static float GetAxis(string name, bool raw)
107         {
108             
return activeInput.GetAxis(name, raw);
109         }
110
111
112         
// -- Button handling --
113         
public static bool GetButton(string name)
114         {
115             
return activeInput.GetButton(name);
116         }
117
118
119         
public static bool GetButtonDown(string name)
120         {
121             
return activeInput.GetButtonDown(name);
122         }
123
124
125         
public static bool GetButtonUp(string name)
126         {
127             
return activeInput.GetButtonUp(name);
128         }
129
130
131         
public static void SetButtonDown(string name)
132         {
133             activeInput.SetButtonDown(name);
134         }
135
136
137         
public static void SetButtonUp(string name)
138         {
139             activeInput.SetButtonUp(name);
140         }
141
142
143         
public static void SetAxisPositive(string name)
144         {
145             activeInput.SetAxisPositive(name);
146         }
147
148
149         
public static void SetAxisNegative(string name)
150         {
151             activeInput.SetAxisNegative(name);
152         }
153
154
155         
public static void SetAxisZero(string name)
156         {
157             activeInput.SetAxisZero(name);
158         }
159
160
161         
public static void SetAxis(string name, float value)
162         {
163             activeInput.SetAxis(name,
value);
164         }
165
166
167         
public static Vector3 mousePosition
168         {
169             
get { return activeInput.MousePosition(); }
170         }
171
172
173         
public static void SetVirtualMousePositionX(float f)
174         {
175             activeInput.SetVirtualMousePositionX(f);
176         }
177
178
179         
public static void SetVirtualMousePositionY(float f)
180         {
181             activeInput.SetVirtualMousePositionY(f);
182         }
183
184
185         
public static void SetVirtualMousePositionZ(float f)
186         {
187             activeInput.SetVirtualMousePositionZ(f);
188         }
189
190
191         
// virtual axis and button classes - applies to mobile input
192         
// Can be mapped to touch joysticks, tilt, gyro, etc, depending on desired implementation.
193         
// Could also be implemented by other input devices - kinect, electronic sensors, etc
194         
public class VirtualAxis
195         {
196             
public string name { get; private set; }
197             
private float m_Value;
198             
public bool matchWithInputManager { get; private set; }
199
200
201             
public VirtualAxis(string name)
202                 :
this(name, true)
203             {
204             }
205
206
207             
public VirtualAxis(string name, bool matchToInputSettings)
208             {
209                 
this.name = name;
210                 matchWithInputManager = matchToInputSettings;
211             }
212
213
214             
// removes an axes from the cross platform input system
215             
public void Remove()
216             {
217                 UnRegisterVirtualAxis(name);
218             }
219
220
221             
// a controller gameobject (eg. a virtual thumbstick) should update this class
222             
public void Update(float value)
223             {
224                 m_Value =
value;
225             }
226
227
228             
public float GetValue
229             {
230                 
get { return m_Value; }
231             }
232
233
234             
public float GetValueRaw
235             {
236                 
get { return m_Value; }
237             }
238         }
239
240         
// a controller gameobject (eg. a virtual GUI button) should call the
241         
// 'pressed' function of this class. Other objects can then read the
242         
// Get/Down/Up state of this button.
243         
public class VirtualButton
244         {
245             
public string name { get; private set; }
246             
public bool matchWithInputManager { get; private set; }
247
248             
private int m_LastPressedFrame = -5;
249             
private int m_ReleasedFrame = -5;
250             
private bool m_Pressed;
251
252
253             
public VirtualButton(string name)
254                 :
this(name, true)
255             {
256             }
257
258
259             
public VirtualButton(string name, bool matchToInputSettings)
260             {
261                 
this.name = name;
262                 matchWithInputManager = matchToInputSettings;
263             }
264
265
266             
// A controller gameobject should call this function when the button is pressed down
267             
public void Pressed()
268             {
269                 
if (m_Pressed)
270                 {
271                     
return;
272                 }
273                 m_Pressed =
true;
274                 m_LastPressedFrame = Time.frameCount;
275             }
276
277
278             
// A controller gameobject should call this function when the button is released
279             
public void Released()
280             {
281                 m_Pressed =
false;
282                 m_ReleasedFrame = Time.frameCount;
283             }
284
285
286             
// the controller gameobject should call Remove when the button is destroyed or disabled
287             
public void Remove()
288             {
289                 UnRegisterVirtualButton(name);
290             }
291
292
293             
// these are the states of the button which can be read via the cross platform input system
294             
public bool GetButton
295             {
296                 
get { return m_Pressed; }
297             }
298
299
300             
public bool GetButtonDown
301             {
302                 
get
303                 {
304                     
return m_LastPressedFrame - Time.frameCount == -1;
305                 }
306             }
307
308
309             
public bool GetButtonUp
310             {
311                 
get
312                 {
313                     
return (m_ReleasedFrame == Time.frameCount - 1);
314                 }
315             }
316         }
317     }
318 }


Gõ tìm kiếm nhanh...